ARG PHP_VERSION
FROM ${PHP_VERSION}

LABEL maintainer="Jhou Shuai <jhou.shuai@qq.com>"

ARG TZ
ARG PHP_EXTENSIONS
ARG CONTAINER_PACKAGE_URL
ENV PHP_MEMORY_LIMIT 256M
ENV MAX_UPLOAD 50M
ENV PHP_MAX_FILE_UPLOAD 200
ENV PHP_MAX_POST=100M
ENV PHP_INI_DIR=/usr/local/etc/php
#使用国内的源防止超时
#RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories
RUN if [ $CONTAINER_PACKAGE_URL ] ; then sed -i "s/dl-cdn.alpinelinux.org/${CONTAINER_PACKAGE_URL}/g" /etc/apk/repositories ; fi


#安装基础服务
RUN apk add --no-cache --upgrade bash && \
    apk add zip unzip tree

RUN apk update && apk add tzdata git supervisor nginx curl vim \
           m4 autoconf make gcc g++ linux-headers \
               imagemagick-dev libmcrypt-dev zlib-dev libpng-dev libzip-dev libwebp-dev jpeg-dev libjpeg-turbo-dev freetype-dev \
               libintl icu icu-dev oniguruma oniguruma-dev openssl openssl-dev

RUN cp /usr/share/zoneinfo/${TZ} /etc/localtime && \
  echo "${TZ}" > /etc/timezone

# Configure PHP-FPM
RUN rm -rf ${PHP_INI_DIR}-fpm.d/www.conf
COPY config/php-fpm.d/www.conf ${PHP_INI_DIR}-fpm.d/www.conf
RUN cp ${PHP_INI_DIR}/php.ini-production ${PHP_INI_DIR}/php.ini
RUN sed -i "s|;date.timezone =.*|date.timezone = ${TZ}|" ${PHP_INI_DIR}/php.ini && \
    sed -i "s|memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|" ${PHP_INI_DIR}/php.ini && \
    sed -i "s|upload_max_filesize =.*|upload_max_filesize = ${MAX_UPLOAD}|" ${PHP_INI_DIR}/php.ini && \
    sed -i "s|max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|" ${PHP_INI_DIR}/php.ini && \
    sed -i "s|post_max_size =.*|post_max_size = ${PHP_MAX_POST}|" ${PHP_INI_DIR}/php.ini && \
    sed -i "s|expose_php =.*|expose_php = Off|" ${PHP_INI_DIR}/php.ini && \
    sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" ${PHP_INI_DIR}/php.ini && \
    sed -i '$a swoole.use_shortname="Off"' ${PHP_INI_DIR}/php.ini

RUN docker-php-ext-configure gd --with-webp=/usr/include  --with-jpeg=/usr/include --with-freetype=/usr/include
RUN docker-php-ext-install -j$(nproc)  gd pdo_mysql opcache bcmath pcntl zip intl


RUN pecl install redis \
    && pecl install mongodb \
    && pecl install https://pecl.php.net/get/swoole-4.8.8.tgz \
    && pecl install xlswriter \
    && pecl install mcrypt \
    && pecl install imagick \
    && docker-php-ext-enable redis mongodb swoole xlswriter mcrypt imagick

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# RUN composer config -g repo.packagist composer https://mirrors.aliyun.com/composer

RUN apk --no-cache add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data

RUN  apk del tzdata && \
      rm -rf /var/cache/apk/* \

WORKDIR /var/www
ENTRYPOINT ["php-fpm"]